package com.glory.cqaospda.view.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.LotInfoBean
import com.glory.cqaospda.databinding.HomeFragmentBinding
import com.glory.cqaospda.view.EmptyPassingStationActivity
import com.glory.cqaospda.view.MswitchActivity
import com.glory.cqaospda.view.SkipDialogActivity
import com.glory.cqaospda.view.StartjobActivity
import com.glory.cqaospda.viewmodel.HomeFragmentModel
import com.glory.mvvmnet.base.BaseFragment
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers

class HomeFragment : BaseFragment<HomeFragmentModel>() {

    lateinit var binding: HomeFragmentBinding
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    activity?.let { it1 -> intent.setClass(it1, clazz) }
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun layoutId(): Int {
        return R.layout.home_fragment
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.home_fragment, container, false)
        return binding.root
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        val instance = MyApp.instance
        instance!!.handler = handler

        binding.homeViewModel = viewModel
        activity?.let { viewModel.initAssets(it,"mfg.txt") }
        viewModel.defUI.callObserve.observe(this) {
            if (!Constant.okgoError) {
                when (it.type) {
                    viewModel.START_ACTIVITY -> {
                        val entity = it.item as FreeUI_TabIndexGridEntity
                        if ("mswitch".equals(entity.picName)) {
                            val intent = Intent()
                            val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                            intent.putExtra("type", "LotID")
                            intent.putExtra("types", "mswitch")
                            activity?.let { it1 -> intent.setClass(it1, clazz) }
                            startActivityForResult(intent, 1)
                        } else if ("startjob".equals(entity.picName)) {
                            val intent = Intent()
                            val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                            intent.putExtra("type", "LotID")
                            intent.putExtra("types", "startjob")
                            activity?.let { it1 -> intent.setClass(it1, clazz) }
                            startActivityForResult(intent, 1)
                        } else {
                            val intent = Intent()
                            intent.putExtra(
                                FreeApi_StaticMembers.SHOW_PLUGIN_KEY,
                                entity.showPlugin
                            )
                            intent.putExtra("Title", entity.text)
                            val clazz = Class.forName(entity.skipClass)
                            activity?.let { it1 -> intent.setClass(it1, clazz) }
                            startActivity(intent)
                        }


                    }
                }
            }else{
                Constant.okgoError = false
            }
        }
    }

    override fun initData() {
        super.initData()

    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                1 -> {
                    val extras = data!!.getExtras()
//                    val lotID = extras!!.getString("lotID").toString()
                    val lotBean = data.getSerializableExtra("lotBean") as ArrayList<LotInfoBean>
                    when(lotBean[0].mIsPassStation){
                        "Y" ->{
                            val intent = Intent()
                            intent.putExtra("lotList", lotBean)
                            intent.putExtra("lotID", lotBean[0].mLotId)
                            activity?.let { it1 -> intent.setClass(it1, EmptyPassingStationActivity::class.java) }
                            startActivity(intent)
                        }
                        "N" ->{
                            val intent = Intent()
                            intent.putExtra("lotID", lotBean[0].mLotId)
                            intent.putExtra("title","开始作业")
                            activity?.let { it1 -> intent.setClass(it1, StartjobActivity::class.java) }
                            startActivity(intent)
                        }
                    }
                }
                2 ->{
                    viewModel.defUI.toastEvent.value = ToastMessageBean("操作成功",true)
                }
            }
        }
    }
}